package me.atbar.moonlib.inventory.page;

import lombok.Getter;
import me.atbar.moonlib.util.ColorUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.List;

/**
 * @author Administrator
 */
public class PageInv {

    @Getter
    private String title;
    @Getter
    private int size;
    @Getter
    private List<ItemStack> list;
    @Getter
    private int preMax;
    @Getter
    private ItemStack before;
    @Getter
    private ItemStack next;

    /**
     * 一个拥有页面的容器
     *
     * @param title 标题
     * @param size 容器大小
     * @param preMax 规定每页最多拥有多少物品
     * @param list 放入的物品集合
     * @param before 上一页
     * @param next 下一页
     */
    public PageInv(String title, int size, int preMax,
                   List<ItemStack> list, ItemStack before, ItemStack next) {
        this.title = title;
        this.size = size;
        this.preMax = preMax;
        this.list = list;
        this.before = before;
        this.next = next;
    }

    public void open(Player player, int page, PageStyle type) {
        int maxPage = list.size() / preMax;
        if(maxPage < page) {
            return;
        }
        Inventory inv = Bukkit.createInventory(new PageHolder(page), size, ColorUtils.color(title));
        if(page < maxPage) {
            inv.setItem(50, next);
        }
        if(page > 0) {
            inv.setItem(48, before);
        }
        for (int i = 0; i < list.size(); i++) {
            if(page > 0) {
                //加了每页中最大物品数
                int hasPageIndex = i + preMax;
                if(hasPageIndex < list.size()) {
                    type.putItem(i, list.get(hasPageIndex));
                }
            } else {
                type.putItem(i, list.get(i));
            }
        }
        player.openInventory(inv);
    }

}
